package product;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

public class SalStock {
	String cod;
	int tot;
	Connection con;
	Date nal;
	String dept;
	void DbCon()throws Exception{
		 Class.forName("oracle.jdbc.driver.OracleDriver");
		 System.out.println("드라이버 로딩 성공...");
	      String url="jdbc:oracle:thin:@192.168.10.4:1521:XE";
	        
	      String user="hr";
	      String pwd="hr";
	        
	     con=DriverManager.getConnection(url,user,pwd);
	      System.out.println("DB 연결 성공!");
	}
	public void buy(String code,int st_tot) throws Exception{
		DbCon();
		Statement st=con.createStatement();
		this.nal=new Date();
		String sql2="select s_code from stock";
		ResultSet rs2=st.executeQuery(sql2);

		String sql="update stock set s_date=sysdate,s_amount=s_amount-"+st_tot+" where s_code= '"+code+"'";
		ResultSet rs=st.executeQuery(sql);
	
		rs.close();
		rs2.close();
		st.close();
		con.close();
	}
	public void update(String code,int pre_ea,int after_ea) throws Exception{
		DbCon();
		
		int a = pre_ea;
		int b = after_ea;
		String co = code;
		String sql="update stock set s_date=sysdate,s_amount= (s_amount+(?-?)) where s_code=?";
		PreparedStatement pstmt=con.prepareStatement(sql);	
		pstmt.setInt(1, b);
		pstmt.setInt(2, a);
		pstmt.setString(3, co);
		
		pstmt.executeUpdate();
	
		pstmt.close();
		con.close();
	} 

	public void recall(String code,int ea)throws Exception{
		DbCon();
		System.out.println(code);
		System.out.println(ea);
		Statement st=con.createStatement();
		
		String sql="update stock set s_date=sysdate,s_amount=s_amount+"+ea+" where s_code='"+code+"'";
		ResultSet rs=st.executeQuery(sql);
		String sql3="Commit";
		ResultSet rs3=st.executeQuery(sql3);
		rs.close();
		rs3.close();
		st.close();
		con.close();
	}

}
